package com.cyzy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cyzy.domain.bean.TPlant;
import com.cyzy.domain.dto.ResponseData;
import com.cyzy.domain.query.TPlantQuery;
import com.cyzy.domain.vo.TPlantVo;
import com.cyzy.mapper.PlantMapper;
import com.cyzy.service.PlantService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class PlantImpl implements PlantService {
    @Autowired
    private PlantMapper plantMapper;

    @Override
    public ResponseData select(TPlantQuery tPlantQuery) {
// 创建分页对象
        Page<TPlantVo> page = new Page<>(tPlantQuery.getCurrentPage(), tPlantQuery.getSize());
        // 使用 QueryWrapper 构造查询条件
        QueryWrapper<TPlantVo> queryWrapper = new QueryWrapper<>();
        if (tPlantQuery.getStatus() != 0) {
            queryWrapper.eq("status", tPlantQuery.getStatus());
        }
        // 查询分页数据
        Page<TPlantVo> plantPage = plantMapper.selectPage(page, queryWrapper);
        // 获取分页数据列表
        List<TPlantVo> data = plantPage.getRecords();

        // 将分页数据封装到 ResponseData 中
        ResponseData responseData = new ResponseData();
        responseData.setData(data);
        responseData.setCode(200);
        return responseData;
    }
//   detail详细数据查询id查询
    @Override
    public ResponseData id(TPlantVo tPlantVo) {
        System.out.println(tPlantVo.getPlantId());

        // 使用 selectById 查询单条数据
        TPlantVo data = plantMapper.selectById(tPlantVo.getPlantId());

        // 将查询到的数据放入集合
        List<TPlantVo> dataList = new ArrayList<>();
        if (data != null) {
            dataList.add(data);
        }

        // 将结果封装到 ResponseData 中
        ResponseData responseData = new ResponseData();
        responseData.setData(dataList);
        responseData.setCode(200);

        return responseData;
    }
//   点击人数加一 修改操作
    @Override
    public ResponseData num(TPlantVo tPlantVo) {
        System.out.println(tPlantVo.getNum());
        // 通过 ID 获取数据库中的对象
        TPlantVo tPlantVor = plantMapper.selectById(tPlantVo.getPlantId());

        // 修改对象的 num 属性值
        tPlantVor.setNum(tPlantVo.getNum());

        // 更新数据库中的对象
        plantMapper.updateById(tPlantVor);

        // 返回成功的响应
        ResponseData responseData = new ResponseData();
        responseData.setCode(200);
        return responseData;
    }
}
